<?php
require_once(LIBPATH.'componentOther/editor.php');

class adminNews extends DivForm
{
	private $fields;
	private $news;
	private $newsValue;
	private $table;

    public function __construct()
    {
        global $news;
        $this->table = $news;
        global $login;
        global $page;
        global $sections;

        $deleteAction = getActionResult('gridNews','remove');

        if ( $deleteAction != '');
        {
        	$this->delete( $deleteAction );
        }

        $fields['title']    = new TextField('title', null, 'Titulo', 120,580, 20, 20, 40, 'text');
        $fields['id']       = new hidden('id');
        //$fields['class']    = new SelectField('class',  array('admin'), 'Classe', 100, 90, 20, 20,60, 'text');

        $flagsArray = $sections->listActive();
        $flagsArray = array_merge( array(''=>''), $flagsArray ? $flagsArray : array() ); //concatenate with empty

        $fields['flags']    = new SelectField('flags', $flagsArray, 'Flag', 120,580, 20, 20, 70, 'text');

        $fields['label']    = new Span('textspan', 'Texto');
        $fields['label']->setBounds(20,100, 100,20);

        $fields['fckEditor']= $editor = new Editor('news');
        $editor->height     = 490;
        $editor->width      = 580;
        $editor->addStyle('position:absolute;');
        $editor->addStyle('left:140px');
        $editor->addStyle('top:100px;');

        $js = "var oEditor = FCKeditorAPI.GetInstance('news') ; document.getElementById('news').value =oEditor.GetHTML();";
        $fields['submitSave'] = new Button('newsSave','Salvar', "$js ajax('gridContainer','adminNews:save')", 'columnTitle');
        $fields['submitSave']->setBounds(630,600,90,30);

        $gridNews   = $this->getGrid();
        $width      = $gridNews->getStyle()->getValue('height');
        $fields['gridContainer']    = new Div('gridContainer', $gridNews);

        $this->fields = $fields; //to use in update

        parent::divForm( 'divNews', $fields, 740,$width+680, "Noticias" );

        $this->setPosition(20,60);
        $this->getBar()->class = 'columnTitle';
        $this->class = 'text';

        if ( $updateAction = getActionResult('gridNews') )
        {
            $this->update($updateAction);
        }
    }

    public function getGrid()
    {
        $list       = $this->table->listAll();

        $columns    = array('id', 'user', 'title', 'regDate', 'regTime', 'flags');
        $widths     = array( 30 ,  100  ,  260   ,  80      ,  80 , 80);

        return new Grid('gridNews', $columns, $list, $widths, 20,650, 24, 'columnTitle', 'text');
    }

    public function delete( $deleteAction )
    {
    	global $page;

    	if ( $this->table->delete( $deleteAction ) )
        {
            $page->addJsCode('alert(\'Exclusao efetuada com sucesso!\')');
        }
    }

    public function update( $updateAction )
    {
        $result = $this->table->get($updateAction);

        foreach ($result as $line => $info)
        {
            $newsValue  = str_replace(array("\r", "\n", "\r\n", "\n\r", '\r', '\n', '\r\n', '\n\r'), '', $info['news'] );
            $newsValue  = str_replace('"', '\'', $newsValue );
            $titleValue = $info['title'];
            $idValue    = $info['id'];
            $flagsValue = $info['flags'];
        }

        $fields = $this->fields;

        $fields['title']->getField()->setValue($titleValue);
        $fields['id']->setValue($idValue);
        $fields['fckEditor']->value = $newsValue;
        //$fields['iClass']->getField()->setValue($classValue);
        $fields['flags']->getField()->setValue($flagsValue);

    }

    public function save()
    {
    	//TODO validators
        global $login;
        global $page;

        $data = $_POST;
        $data['user'] = $login->getUser();

        if ( $this->table->insert($data) )
        {
        	$fields[] = new Script('alert(\'Registro efetuado com sucesso!\')');
        }
        else
        {
            $fields[] = new Script('alert(\'Falha na atualizacão do Registro!\')');
        }

        //clean fields
        $fields[] = new Script("
        $('title').value='';
        $('flags').value='';
        var oEditor = FCKeditorAPI.GetInstance('news') ;
        oEditor.SetHTML('');");

        $fields[] = $this->getGrid();

        return $fields;
    }
}
?>